angular.module("module.goodsStatistic",[])
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider
            .state('home.goodsStatistic',{
                url:"/goodsStatistic",
                templateUrl:"goodsStatistic/goodsStatistic.html",
                controller:'GoodsStatisticController',
                controllerAs:'vm'
            })
    }])
    .controller('GoodsStatisticController',[function () {
        var vm=this;
        function check() {
            winAlert({title:'请验证商品统计密码',body:template('checkPassword'),cancel:'取消',fun:function(){
                var password=$(".panel input").val();
                console.log("password",password);
                if (password == goodsStatisticsPassword){
                    checked=true;
                    $(".panel .close").trigger("click");
                    loadpage()
                }else {
                    return toast('密码不正确，请重新输入')
                }
            }});
        }

        function loadpage() {
            if (!checked){//该判断是为了防止点击分店列表中分店选项直接刷新页面而跳过验证密码的安全问题
                check();
            }else {
                if (!data.pi) data.pi = get()['pi'] || 1;
                if (!data.goodsName) data.goodsName = get()['goodsId'] || '';
                console.log(data);
                $('.container-fluid').html(template('list_tmpl', data));

                $('.datetime').daterangepicker({
                    format: 'YYYY-MM-DD HH:mm', timePicker: true,
                    timePickerSeconds: true, timePickerIncrement: 1, timePicker12Hour: false
                }, function (start, end) {
                    data.startTime = start.format('YYYY-MM-DD HH:mm');
                    data.endTime = end.format('YYYY-MM-DD HH:mm');
                    $(':input[name=startTime]').val(data.startTime);
                    $(':input[name=endTime]').val(data.endTime);
                });

                $('.daterang').on('click', function () {
                    var day = parseInt($(this).attr('data-day'), 10);
                    data.startTime = moment().subtract(day, 'days').format('YYYY-MM-DD 00:00');
                    data.endTime = moment().format('YYYY-MM-DD HH:mm');
                    $(':input[name=startTime]').val(data.startTime);
                    $(':input[name=endTime]').val(data.endTime);
                    return false;
                });

                $('.search').on('click', function () {
                    ajaxtodata();
                    return false;
                });

                $('.pagination a').off().on('click', function () {
                    data.pi = $(this).attr('href');
                    if (!data.pi) return false;
                    ajaxtodata();
                    return false;
                });

                $('.print').on('click', function () {
                    winAlert({
                        body: '您确定要打印统计数据么', cancel: '取消', fun: function () {
                            return $.getJSON('goodsTJPrint', {
                                goodsId: data.goodsName,
                                startTime: data.startTime,
                                endTime: data.endTime
                            }, function (rs) {
                                if (!rs.succeed) return toast('打印失败，失败原因：' + rs.message);
                                toast('打印成功...', 'success');
                            });
                        }
                    });
                });
                $('.search-shop').on('focus', function () {
                    data.seachShop = '';
                    $('.shop-list').show();
                    selectlist();
                    return false;
                }).on('keyup', function () {
                    data.seachShop = $.trim($(this).val());
                    selectlist();
                });

                $('.shop-mask').on('click', function () {
                    selectshopid();
                    return false;
                });
            }
        }

        function ajaxtodata(){
//    location.href='goodsTJ?goodsId='+encodeURI(data.goodsName)+'&startTime='+data.startTime+
//        '&endTime='+data.endTime+'&pi='+data.pi;
            var para={};
            para.goodsId=encodeURI(data.goodsName);
            para.startTime=data.startTime;
            para.endTime=data.endTime;
            para.shopId=shopId;
            para.pi=data.pi;
            $.getJSON("goodsTJList",para,function (res) {
                data=res.data;
                loadpage();
            })
        }

        function get(url){
            url || (url = location.search.slice(1));
            var arr = url.split("&"), get={}, tem;
            for(var i=0; i < arr.length; i++){
                tem = arr[i].split("=");
                get[tem[0]] = decodeURI(tem[1]);
            }
            return get;
        }

        //$('.search-shop').on('focus', function(){
        //    data.seachShop = '';
        //    $('.shop-list').show();
        //    selectlist();
        //    return false;
        //}).on('keyup', function(){
        //    data.seachShop = $.trim($(this).val());
        //    selectlist();
        //});

        function selectlist(){
            $('.shop-list .list-group').html(template('shop-list-tpml', data));
            $('.shop-list .list-group-item').off().on('click', function(){
                if ($(this).attr('data-id')){
                    $(this).addClass('active').siblings().removeClass('active');
                } else {
                    $('.shop-list .list-group-item').removeClass('active');
                }
                selectshopid();
                return false;
            });
        }

        //$('.shop-mask').on('click', function(){
        //    selectshopid();
        //    return false;
        //});

        function selectshopid(){
            $('.shop-list').hide();
            var active = $('.shop-list .active');
            var id = active.attr('data-id');
            $('.search-shop').val(id?active.html():'');
            data.goodsName=id||'';
        }
        selectlist();
        selectshopid();
        check()
    }]);